<?php

$__FILEPATH__ = dirname(__FILE__)."/";
require_once($__FILEPATH__."./func_common.php");
require_once($__FILEPATH__."./class_def.php");
require_once($__FILEPATH__."./conf/info.php");
session_start();
if (!isset($_SESSION["user"]))
{
	header("Location: ./loginpage.php");
	exit(0);
}
header("Content-Type: text/html; charset=UTF-8");

$BlogItem = new BlogListItem();
$mysqli = GetMysqliInstance();

$BlogItem = new BlogListItem();
$username = $_SESSION["user"];

$BID = 1;
if (isset($_GET["bid"]))
	$BID = intval($_GET["bid"]);
else if (isset($_POST["id"]))
	$BID = intval($_POST["id"]);

$AlgoList = array();
$AlgoMap = array();

$result = $mysqli->query("select id,name from classlist where type='algorithm'", MYSQLI_USE_RESULT);
if ($result)
{
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$ClassItem = new ClassListItem();
		$ClassItem->id = $row["id"];
		$ClassItem->name = $row["name"];
		$AlgoList[] = $ClassItem;
	}
	$result->close();
}

$result = $mysqli->query("select cid from bclinklist where bid = $BID", MYSQLI_USE_RESULT);
if ($result)
{
	while($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$id = $row["cid"];
		$AlgoMap[$id] = -1;
	}
	$result->close();
}

if (isset($_POST["blog_update"]))
{
	if (!isset($_POST["id"], $_POST["title"], $_POST["keyword"], $_POST["editor"]))
	{
		$mysqli->close();
		die("非法操作2~");
	}

	$bid = intval($_POST["id"]);

	$result = $mysqli->query("select user from bloglist where id = $bid");
	if ($result && $row = $result->fetch_array(MYSQLI_ASSOC) && $row["user"] == $uesrname) $result->close();
	else{
		$result->close();
		$mysqli->close();
		die("非法操作1~");
	} 

	$title = $_POST["title"];
	$keyword = $_POST["keyword"]."|";
	$content = $_POST["editor"];

	$stmt = $mysqli->prepare("update bloglist set title=?, keyword=?, content=? where id = $bid");
	$stmt->bind_param("sss", $title, $keyword, $content);
	if (!$stmt->execute())
	{
		$stmt->close();
		$mysqli->close();
		die("系统出错~");
	}
	$stmt->close();

	$class_select = false;
	foreach ($AlgoList as $ListItem){
		if (isset($_POST["$ListItem->id"]))
		{
			$class_select = true;
			if (isset($AlgoMap[$ListItem->id]))
				++$AlgoMap[$ListItem->id];
			else 
				$AlgoMap[$ListItem->id] = 1;
		}

		if (isset($AlgoMap[$ListItem->id]) && $AlgoMap[$ListItem->id] != 0)
		{
			if ($AlgoMap[$ListItem->id] > 0)
			{
				$mysqli->query("insert into bclinklist values($bid, $ListItem->id)");
				$mysqli->query("update classlist set cnt = cnt + 1 where id = $ListItem->id");
			}
			else
			{
				$mysqli->query("delete from bclinklist where bid = $bid and cid = $ListItem->id");
				$mysqli->query("update classlist set cnt = cnt - 1 where id = $ListItem->id");
			}
		}
	}

	if ($class_select)
	{
		$mysqli->query("delete from bclinklist where bid = $bid and cid = 0");
	}
	else 
	{
		if (!isset($AlgoMap[0]))
			$mysqli->query("insert into bclinklist values($bid, 0)");
	}

	$mysqli->close();
	ClientRedirect("./blog_display.php?bid=$bid", "更新成功~");
	exit(0);
}



$ret = false;

$result = $mysqli->query("select * from BlogList where id = $BID && user='$username'", MYSQLI_USE_RESULT);

if ($result)
{
	if ($row = $result->fetch_array(MYSQLI_ASSOC))
	{
		$ret = true;
		$BlogItem->id = $row["id"];
		$BlogItem->title = $row["title"];
		$BlogItem->content = $row["content"];
		$BlogItem->user = $row["user"];
		$BlogItem->keyword = substr($row["keyword"], 0, strlen($row["keyword"]) - 1);
		$BlogItem->in_ip = $row["in_ip"];
		$BlogItem->in_time = $row["in_time"];
		$BlogItem->permission = $row["permission"];
		$BlogItem->classlist = array();
	}
	$result->close();
}

$mysqli->close();

if (!$ret)
{
	die("非法操作~");
}

?>